<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2020/2/13
 * Time: 14:39
 */

namespace app\admin\controller;


use think\Db;
use think\Request;

class Match extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [];
            if(isset($user_name) && $user_name != ''){
                $where['mf_name|mf_home_name_cn|mf_away_name_cn|mf_fj_matchid'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($mf_state) && $mf_state != ''){
                if($mf_state == 1) $where['mf_state'] = 0;
                if($mf_state == 2) $where['mf_state'] = ['>',0];
                if($mf_state == 3) $where['mf_state'] = -1;
                if($mf_state == 4) $where['mf_state'] = ['<',-1];
            }
            if(isset($mf_fid) && $mf_fid != ''){
                $where['mf_fid'] = $mf_fid;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_match_fj')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_match_fj')
                ->field('mf_json',true)
                ->where($where)
                ->page($NowPage,$limits)
                ->order('mf_match_time desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 详情
     * @return mixed
     */
    public function matchInfo(){
        $mf_id = input('mf_id');
        $data = Db::name('match_fj')
            ->where('mf_id',$mf_id)
            ->find();
        $json = json_decode($data['mf_json'],true);
        if($data['mf_echarts']){
            //43.进攻  44.危险进攻   14.控球率
            $arrAll = explode(';',$data['mf_echarts']);
            foreach ($arrAll as $k => $v){
                $arrSon = explode(',',$v);
                if($arrSon[0] == 14){
                    $data['echRight1'] = str_replace('%','',$arrSon[1]);
                    $data['echRight2'] = str_replace('%','',$arrSon[2]);
                }
                if($arrSon[0] == 43){
                    $data['echLeft1'] = $arrSon[1];
                    $data['echLeft2'] = $arrSon[2];
                }
                if($arrSon[0] == 44){
                    $data['echMiddle1'] = $arrSon[1];
                    $data['echMiddle2'] = $arrSon[2];
                }
            }
        }
        $state1 = [
            '' => '',
            0 => '未开',
            1 => '上半场',
            2 => '中场',
            3 => '下半场',
            4 => '加时',
            5 => '点球',
            -1 => '完场',
            -10 => '取消',
            -11 => '待定',
            -12 => '腰斩',
            -13 => '中断',
            -14 => '推迟'
        ];
        $state2 = [
            '' => '',
            '0' => '未开赛',
            '1' => '一节',
            '2' => '二节',
            '3' => '三节',
            '4' => '四节',
            '5' => '1OT',
            '6' => '2OT',
            '7' => '3OT',
            '50' => '中场',
            '-1' => '完场',
            '-2' => '待定',
            '-3' => '中断',
            '-4' => '取消',
            '-5' => '推迟',
        ];
        $this->assign([
            'info' => $data,
            'json' => $json,
            'state1' => $state1,
            'state2' => $state2,
        ]);
        return $this->fetch('match/matchinfo');
    }

    /**
     * 查看讨论
     * mf_fj_matchid
     * mt_id
     * @return mixed|\think\response\Json
     */
    public function talks(){
        extract(input());
        $where = [
            'mt_fj_matchid' => $mf_fj_matchid
        ];
        $field = 'mt.*,user_username,user_image,user_phone';
        $reply = Db::table('up_match_talk')->alias('mt')
            ->join('up_u_user u','mt.mt_user_id = u.user_id','left')
            ->where($where)
            ->order('mt_id asc')
            ->field($field)
            ->select();
        $this->assign([
            'reply' => $reply
        ]);
        return $this->fetch();
    }
    /**
     * 查看直播文字
     * mf_fj_matchid
     * mtf_id
     * @return mixed|\think\response\Json
     */
    public function lives(){
        extract(input());
        $where = [
            'mtf_fj_matchid' => $mf_fj_matchid
        ];
        $field = 'mtf_id,mtf_content,mtf_time';
        $reply = Db::table('up_match_talk_fj')
            ->where($where)
            ->order('mtf_id asc')
            ->field($field)
            ->select();
        $this->assign([
            'reply' => $reply
        ]);
        return $this->fetch();
    }
}